<template>
  <div class="topbar">
    <!-- 版心 -->
    <div class="wrap wrapTopbar">
      <!-- 左侧标题 -->
      <div class="l">欢迎来到叩丁狼积分商城</div>
      <!-- 右侧菜单 -->
      <ul class="r">
        <li>
          <img src="../assets/img/userImg.f8bbec5e.png" class="avator" alt="" />
          用户名：游客
        </li>
        <li>我的积分：--</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>

        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>9</strong>
        </li>
        <li class="login-btn" @click="changeIsShowLoginModal(true)" v-else>
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex';
import { WeixinLoginAPI } from '@/request/api';
export default {
  //钩子函数！！！！！
  created() {
    //只要用户扫码了，页面会重新刷新 ，此时可以获取下code值
    //[!!!有可能会获取不到code...$nextTick()!!!!]
    this.$nextTick(async () => {
      console.log('this.$route', this.$route.query.code);
      let mycode = this.$route.query.code;
      if (!mycode) return;
      //当有code时，就可以发起微信扫码登录请求
      let res = await WeixinLoginAPI({
        code: mycode,
      });
      console.log('WeixinLoginAPI:', res);
      //0 400 407
      if (res.code == 0) {
        //1 . 提示登录成功
        alert('登录成功');
        //2. 存储token
        localStorage.setItem('x-auth-token', res['x-auth-token']);
        //3. 改变登录状态 （切换购物车显示）
        this.changeIsLogin(true);
      } else if (res.code == 400) {
        //1. 提示二维码失效
        alert('请重新扫码二维码');
        //2. 把登录模态框打开
        this.changeIsShowLoginModal(true);
      } else if (res.code == 407) {
        //1.提示用户再使用手机号验证码登录一次
        alert('请使用手机号验证码登录一次');
        //2. 把登录模态框打开
        this.changeIsShowLoginModal(true);
        //3. 存储uuid （登录时取）
        localStorage.setItem('uuid', res.uuid);
      }
    });
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
    }),
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: 'showModal/changeIsShowLoginModal',
      changeIsLogin: 'loginStatus/changeIsLogin',
    }),
  },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  background: #242b39;
  font-size: 14px;
  font-family: SourceHanSansSC;
  font-weight: 300;
  color: #fffefe;
  line-height: 40px;
  .wrapTopbar {
    display: flex;
    justify-content: space-between;
    .r {
      li {
        float: left;
        margin-left: 20px;
        cursor: pointer;
        text-align: center;
        &.login-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
        }
        &.cart-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
          img {
            width: 20px;
            height: 19px;
            vertical-align: middle;
          }
          span {
            margin: 0 8px;
          }
          strong {
            width: 22px;
            height: 22px;
            line-height: 22px;
            display: inline-block;
            border-radius: 50%;
            background-color: #fd604d;
          }
        }
      }
      .avator {
        width: 22px;
        vertical-align: middle;
        border-radius: 50%;
      }
    }
  }
}
</style>
